/**
 * 将文件内容读取为 Base64 字符串
 * @param file 文件对象
 * @returns Promise<string> Base64 编码的文件内容
 */
export const readFileAsBase64 = (file: File): Promise<string> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()

    reader.onload = () => {
      if (typeof reader.result === 'string') {
        // 移除 Data URL 的前缀，只保留 Base64 内容
        const base64Content = reader.result.split(',')[1]
        resolve(base64Content)
      } else {
        reject(new Error('Failed to read file as Base64'))
      }
    }

    reader.onerror = () => {
      reject(reader.error)
    }

    reader.readAsDataURL(file)
  })
}
